﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Volo.Abp.AspNetCore.Mvc;
using YAPACS.RisServer.Abstractions;
using YAPACS.RisServer.Services.Dtos;
using YAPACS.Workflows;

namespace YAPACS.RisServer.Controllers;

[Authorize]
public class TechnicianController(ITechnicianService technicianService, IReportService reportService) : AbpController
{
    private readonly ITechnicianService _technicianService = technicianService;
    private readonly IReportService _reportService = reportService;


    /// <summary>
    /// 获取高级搜索视图
    /// </summary>
    /// <returns></returns>
    [HttpGet]
    [Route("api/technician/get-advanced-search-view")]
    public async Task<IActionResult> GetAdvancedSearchViewAsync()
    {
        var result = await _technicianService.GetAdvancedSearchViewAsync();
        return Ok(result);

    }
    /// <summary>
    /// 快速查询检查列表
    /// </summary>
    /// <param name="filterId"></param>
    /// <param name="take"></param>
    /// <param name="skip"></param>
    /// <param name="searchValue"></param>
    /// <returns></returns>
    [HttpPost]
    [Route("api/technician/get-list")]
    public async Task<IActionResult> GetExamListByFilterAsync([FromBody] DataGridRequest request)
    {
        var result = await _technicianService.GetExamListByFilterAsync(request);
        return Ok(result);
    }
    /// <summary>
    /// 查看患者基本信息
    /// </summary>
    /// <param name="examId"></param>
    /// <returns></returns>
    [HttpGet]
    [Route("api/technician/view-apply/{examId}")]
    public async Task<IActionResult> ViewApplyAsync(Guid examId)
    {
        var result = await _technicianService.ViewApplyAsync(examId);
        return Ok(result);

    }
    /// <summary>
    /// 更换设备
    /// </summary>
    /// <param name="examId"></param>
    /// <param name="deviceId"></param>
    /// <returns></returns>
    [HttpGet]
    [Route("api/technician/change-device/exam/{examId}/device/{deviceId}")]
    public async Task<IActionResult> ChangeDeviceAsync(Guid examId, Guid deviceId)
    {
        var result = await _technicianService.ChangeDeviceAsync(examId, deviceId);
        return Ok(result);

    }
    /// <summary>
    /// 呼叫患者（具体患者）
    /// </summary>
    /// <param name="examId"></param>
    /// <returns></returns>
    [HttpGet]
    [Route("api/technician/call/{examId}")]
    public async Task<IActionResult> CallAsync(Guid examId)
    {
        var result = await _technicianService.CallAsync(examId);
        return Ok(result);

    }
    /// <summary>
    /// 呼叫下一位
    /// </summary>
    /// <returns></returns>
    [HttpGet]
    [Route("api/technician/call-next")]
    public async Task<IActionResult> CallNextAsync()
    {
        var result = await _technicianService.CallNextAsync();
        return Ok(result);

    }
    /// <summary>
    /// 开始检查
    /// </summary>
    /// <param name="examId"></param>
    /// <returns></returns>
    [HttpGet("api/technician/start-exam/{examId}")]
    public async Task<IActionResult> StartAsync(Guid examId)
    {
        var result = await _technicianService.StartAsync(examId);
        return Ok(result);

    }
    [HttpGet("api/technician/{examId}/start-exam")]
    public async Task<IActionResult> StartExamAsync(Guid examId)
    {
        var result = await _technicianService.StartAsync(examId);
        if (result.Success)
        {
            result = await _reportService.WriteReportAsync(examId);
        }
        return Ok(result);
    }
    /// <summary>
    /// 结束检查
    /// </summary>
    /// <param name="examId"></param>
    /// <returns></returns>
    [HttpGet]
    [Route("api/technician/completed-exam/{examId}")]
    public async Task<IActionResult> CompletedAsync(Guid examId)
    {
        var result = await _technicianService.CompletedAsync(examId);
        return Ok(result);

    }
    /// <summary>
    /// 检查参数记录
    /// </summary>
    /// <param name="examId"></param>
    /// <returns></returns>
    [HttpPost]
    [Route("api/technician/check-paramter-record")]
    public async Task<IActionResult> CheckParameterRecordAsync([FromBody] Input input)
    {
        var result = await _technicianService.CheckParameterRecordAsync(input.ExamId);
        return Ok(result);

    }
    /// <summary>
    /// 紧急登记
    /// </summary>
    /// <returns></returns>
    [HttpPost]
    [Route("api/technician/emergency-regist")]
    public async Task<IActionResult> EmergencyRegistAsync()
    {
        var result = await _technicianService.EmergencyRegisterAsync();
        return Ok(result);
    }
    /// <summary>
    /// 重新检查
    /// </summary>
    /// <param name="examId"></param>
    /// <returns></returns>
    [HttpPost]
    [Route("api/technician/recheck")]
    public async Task<IActionResult> RecheckAsync([FromBody] Input input)
    {
        var result = await _technicianService.RecheckAsync(input.ExamId);
        return Ok(result);
    }
    /// <summary>
    /// 取消检查
    /// </summary>
    /// <param name="examId"></param>
    /// <returns></returns>
    [HttpPost]
    [Route("api/technician/cancel-exam")]
    public async Task<IActionResult> CancelExamAsync([FromBody] WorkflowInput input)
    {
        var result = await _technicianService.CancelExamAsync(input, "/api/technician/cancel-exam");
        return Ok(result);
    }
    [HttpPost("api/technician/pass")]
    public async Task<IActionResult> PassExamAsync([FromBody] Input input)
    {
        var result = await _technicianService.PassAsync(input.ExamId);
        return Ok(result);
    }
    [HttpPost("api/technician/export")]
    public async Task<IActionResult> Export([FromBody] DataGridRequest request)
    {
        var result = await _technicianService.ExportAsync(request);
        return Ok(result);
    }
}
